package com.np.media1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;

import java.io.IOException;

public class TextureViewActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener {

    private TextureView textureView1;
    private ImageView imageView1;
    private Surface surface1;
    private MediaPlayer mediaPlayer1;
    private static String path = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";

    public TextureViewActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texture_view);

        textureView1 = findViewById(R.id.textureView1);
        imageView1 = findViewById(R.id.imageView1);

        init();
    }

    private void init() {
        textureView1.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                surface1 = new Surface(surface);
                try {
                    play();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                textureView1 = null;
                surface1 = null;
                mediaPlayer1.stop();
                mediaPlayer1.release();
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
    }

    private void play() throws IOException {
        mediaPlayer1 = new MediaPlayer();

        mediaPlayer1.setDataSource(getApplicationContext(), Uri.parse(path));
        mediaPlayer1.setSurface(surface1);
        mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mediaPlayer1.setOnPreparedListener(this);
        mediaPlayer1.setOnInfoListener(this);
        mediaPlayer1.setOnBufferingUpdateListener(this);

        mediaPlayer1.prepareAsync();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        imageView1.setVisibility(View.GONE);
        mediaPlayer1.start();
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

    }
}